package edu.ntu.helper;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class StoryXMLHandler extends DefaultHandler {

	Boolean currentElement = false;
	String currentValue = null;
	public static StoryList storyList = null;

	public static StoryList getStoryList() {
		return storyList;
	}

	public static void setStoryList(StoryList storyList) {
		StoryXMLHandler.storyList = storyList;
	}

	/**
	 * Called when tag starts ( ex:- <name>AndroidPeople</name> -- <name> )
	 */
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if (localName.equalsIgnoreCase("ind_and_storiess"))
		{
			storyList = new StoryList();
		}
	}

	/**
	 * Called when tag closing ( ex:- <name>AndroidPeople</name> -- </name> )
	 */
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {

		/** set value */
		if (localName.equalsIgnoreCase("story_index"))
		{
			storyList.setIndex(Integer.parseInt(currentValue));
		}
		else if (localName.equalsIgnoreCase("pic_link"))
			storyList.setLink(currentValue);
		else if (localName.equalsIgnoreCase("submit_time"))
			storyList.setSubmit_time(currentValue);
		else if (localName.equalsIgnoreCase("story_tags"))
			storyList.setTags(currentValue);
		else if (localName.equalsIgnoreCase("story_title"))
			storyList.setTitle(currentValue);
		else if (localName.equalsIgnoreCase("story_Details"))
			storyList.setDetails(currentValue);

	}

	/**
	 * Called to get tag characters ( ex:- <name>AndroidPeople</name> -- to get
	 * AndroidPeople Character )
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
			currentValue = new String(ch, start, length);
	}

}